package com.hctforgreen.greenservice;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.taptwo.android.widget.MyXListView;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import cn.dns.reader.network.Util;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.hctforgreen.greenservice.ctr.DbController;
import com.hctforgreen.greenservice.db.DBManager;
import com.hctforgreen.greenservice.encoding.RGBLuminanceSource;
import com.hctforgreen.greenservice.model.LoginResultEntity;
import com.hctforgreen.greenservice.model.ModuleforRefrigerantEnum;
import com.hctforgreen.greenservice.model.PwdEntity;
import com.hctforgreen.greenservice.model.MachineListEntity.MachineEntity;
import com.hctforgreen.greenservice.ui.adapter.HisPwdListAdapter;
import com.hctforgreen.greenservice.ui.adapter.PwdListAdapter;
import com.hctforgreen.greenservice.ui.adapter.SpinnerStringsAdapter;
import com.hctforgreen.greenservice.utils.DecodeBitmapUtil;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.ImageCropUtil;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;
import com.hctforgreen.greenservice.utils.RefrigerantCalculatorUtil;
import com.hctforgreen.greenservice.utils.Utils;

/**
 * 冷媒计算
 * 
 * @author wuhs 20151208
 * 
 */
public class RefrigerantCalculatorActivity extends ParentActivity {
	/**
	 * 冷媒计算器的请求码
	 */
	public static final int REQUEST_CODE = 3099;
	private Activity context;
	private String barcode;
	public  final int ZXING_DATA = 3024;
	private final int FLAG_MODIFY_FINISH = 3025;
	
	private TextView view_titleTv;
	private ImageButton button_back;
	/**
	 * 条码
	 * 条码只作用于判断机型，只需判断前5位，不作其它格式限制
	 */
	private EditText code_edit;
	private Button scan_btn,pic_btn,sub_btn;
	/**
	 * 室外机模块系列
	 */
	private Spinner Spinner_module_name;
	/**
	 * 各模块额定容量
	 */
	private Spinner spinner_module_capacity,spinner_module_capacity_1,spinner_module_capacity_2,spinner_module_capacity_3,spinner_module_capacity_4;
	/**
	 * 是否为全部内机
	 */
	private CheckBox checkbox_isall_inner_module;
	/**
	 * 内机总台数
	 */
	private EditText editText_inner_count;
	/**
	 * 内机总额定容量
	 */
	private EditText editText_inner_capacityCount;
	/**
	 * 连接管长度
	 */
	private EditText row_text1,row_text2,row_text3,row_text4,row_text5,row_text6,row_text7,row_text8;
	
	private LinearLayout layout_out_module_message,layout_module_capacity_1,layout_module_capacity_2,layout_inner_module_message,layout_inner_module_message_details,layout_ioConnectionLength_details;
	private TableRow layout_row_text6;
	
	//外机额定容量适配器
	private SpinnerStringsAdapter adapter_es,adapter_5s,adapter_tops,adapter_star;
	
	@Override
	protected void onResume() {
		super.onResume();
		initSkinLayout();
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.activity_refrigerant_calculator);
		
		barcode = getIntent().getStringExtra("barcode");
		
		findView();
		setListensers();
		initViewValue();
		
	}

	/**
	 * 获取组件id
	 */
	private void findView(){
		layout_out_module_message = (LinearLayout)findViewById(R.id.layout_out_module_message);
		layout_module_capacity_1 = (LinearLayout)findViewById(R.id.layout_module_capacity_1);
		layout_module_capacity_2 = (LinearLayout)findViewById(R.id.layout_module_capacity_2);
		layout_inner_module_message = (LinearLayout)findViewById(R.id.layout_inner_module_message);
		layout_inner_module_message_details = (LinearLayout)findViewById(R.id.layout_inner_module_message_details);
		layout_ioConnectionLength_details = (LinearLayout)findViewById(R.id.layout_ioConnectionLength_details);
		layout_row_text6 = (TableRow)findViewById(R.id.layout_row_text6);
		
		view_titleTv = (TextView) findViewById(R.id.tv_title);
		button_back = (ImageButton) findViewById(R.id.btn_back);
		code_edit = (EditText)findViewById(R.id.code_edit);
		scan_btn  = (Button)findViewById(R.id.scan_btn);
		pic_btn = (Button)findViewById(R.id.pic_btn);
		sub_btn = (Button)findViewById(R.id.sub_btn); 
		findViewById(R.id.btn_right_first).setBackgroundResource(R.drawable.btn_common_help_selector);
		
		
		Spinner_module_name = (Spinner)findViewById(R.id.Spinner_module_name);
		spinner_module_capacity = (Spinner)findViewById(R.id.spinner_module_capacity);
		spinner_module_capacity_1 = (Spinner)findViewById(R.id.spinner_module_capacity_1);
		spinner_module_capacity_2 = (Spinner)findViewById(R.id.spinner_module_capacity_2);
		spinner_module_capacity_3 = (Spinner)findViewById(R.id.spinner_module_capacity_3);
		spinner_module_capacity_4 = (Spinner)findViewById(R.id.spinner_module_capacity_4);
		
		checkbox_isall_inner_module = (CheckBox)findViewById(R.id.checkbox_isall_inner_module);
		
		editText_inner_count = (EditText)findViewById(R.id.editText_inner_count);
		editText_inner_capacityCount = (EditText)findViewById(R.id.editText_inner_capacityCount);
		row_text1 = (EditText)findViewById(R.id.row_text1);
		row_text2 = (EditText)findViewById(R.id.row_text2);
		row_text3 = (EditText)findViewById(R.id.row_text3);
		row_text4 = (EditText)findViewById(R.id.row_text4);
		row_text5 = (EditText)findViewById(R.id.row_text5);
		row_text6 = (EditText)findViewById(R.id.row_text6);
		row_text7 = (EditText)findViewById(R.id.row_text7);
		row_text8 = (EditText)findViewById(R.id.row_text8);
		
	}
	
	/**
	 * 组件赋值
	 */
	private void initViewValue(){
		String[] items_es = getResources().getStringArray(R.array.refrigerant_calculator_module_capacity_value_ES);
		String[] items_star = getResources().getStringArray(R.array.refrigerant_calculator_module_capacity_value_Star);
		String[] items_5s = getResources().getStringArray(R.array.refrigerant_calculator_module_capacity_value_5S);
		String[] items_tops = getResources().getStringArray(R.array.refrigerant_calculator_module_capacity_value_Tops);
		adapter_es = new SpinnerStringsAdapter(context, items_es);
		adapter_star = new SpinnerStringsAdapter(context, items_star);
		adapter_5s = new SpinnerStringsAdapter(context, items_5s);
		adapter_tops = new SpinnerStringsAdapter(context, items_tops);
		view_titleTv.setText("计算冷媒追加量");
		if(barcode!=null){
			code_edit.setText(barcode);
		}
		
		initSpinner_module_name();
		initSpinner_module_capacity(0);
	}
	
	/**
	 * 组件绑定事件
	 */
	private void setListensers(){
		button_back.setOnClickListener(mOnClick);
		scan_btn.setOnClickListener(mOnClick);
		pic_btn.setOnClickListener(mOnClick);
		sub_btn.setOnClickListener(mOnClick);
		findViewById(R.id.btn_right_first).setOnClickListener(mOnClick);
		
		Spinner_module_name.setOnItemSelectedListener(mOnItemSelected);
		spinner_module_capacity.setOnItemSelectedListener(mOnItemSelected);
		spinner_module_capacity_1.setOnItemSelectedListener(mOnItemSelected);
		spinner_module_capacity_2.setOnItemSelectedListener(mOnItemSelected);
		spinner_module_capacity_3.setOnItemSelectedListener(mOnItemSelected);
		spinner_module_capacity_4.setOnItemSelectedListener(mOnItemSelected);
		
		checkbox_isall_inner_module.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					layout_inner_module_message_details.setVisibility(View.GONE);
				}else{
					layout_inner_module_message_details.setVisibility(View.VISIBLE);
				}
				
			}
		});
		
		//条码输入框监听事件
		code_edit.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if(s.toString().length()>=5){
					if(!s.toString().substring(0,5).equals(barcode)){
						barcode = s.toString().substring(0,5);
						changeModuleFromBarcode();
					}
				}
			}
		});
	}
	
	private void initSpinner_module_name(){
    	SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(context, ModuleforRefrigerantEnum.getNameList());
		Spinner_module_name.setAdapter(adapter);
		changeModuleFromBarcode();
		
	}
	/**
	 * 初始化外机额定容量下拉框
	 * @param id
	 */
	private void initSpinner_module_capacity(int id){
		
		switch (id) {
		case 1: //ES系列外机
			layout_module_capacity_2.setVisibility(View.GONE);
			layout_module_capacity_1.setVisibility(View.VISIBLE);
			spinner_module_capacity.setAdapter(adapter_es);
			break;
		case 2: //5S系列外机
			layout_module_capacity_2.setVisibility(View.VISIBLE);
			layout_module_capacity_1.setVisibility(View.GONE);
			spinner_module_capacity_1.setAdapter(adapter_5s);
			spinner_module_capacity_2.setAdapter(adapter_5s);
			spinner_module_capacity_3.setAdapter(adapter_5s);
			spinner_module_capacity_4.setAdapter(adapter_5s);
			break;
		case 3: //tops系列外机
			layout_module_capacity_2.setVisibility(View.GONE);
			layout_module_capacity_1.setVisibility(View.VISIBLE);
			spinner_module_capacity.setAdapter(adapter_tops);
			break;
		case 4: 
			layout_module_capacity_2.setVisibility(View.GONE);
			layout_module_capacity_1.setVisibility(View.VISIBLE);
			spinner_module_capacity.setAdapter(adapter_star);
			break;
		default:
			break;
		}
		
		
		
		
	}
	
	
	/**
	 * 根据条码改变模块系列
	 */
	private void changeModuleFromBarcode(){
		String barcode = code_edit.getText().toString();
		if(barcode!=null && !("").equals(barcode)){
			DbController controller = new DbController(context);
			MachineEntity lEntity = controller.getMachineEntity(barcode.substring(
					0, 5));
			if (lEntity == null || lEntity.childSeriesId.isEmpty()) {
				Toast.makeText(context,
						getString(R.string.toast_hasNotModule),
						Toast.LENGTH_SHORT).show();
			}else{
				int Position = ModuleforRefrigerantEnum.getPosition(lEntity.childSeriesId);
				if(Position==0){
					Toast.makeText(context,
							getString(R.string.toast_hasNotModule),
							Toast.LENGTH_SHORT).show();
				}else{
					Spinner_module_name.setSelection(Position);
				}
			}
		}
	}


	private OnItemSelectedListener mOnItemSelected = new OnItemSelectedListener(){

		@Override
		public void onItemSelected(AdapterView<?> AdapterView, View view,
				int position, long id) {
			switch (AdapterView.getId()) {
			case R.id.Spinner_module_name://模块系列选择
				if(id==0){
					layout_out_module_message.setVisibility(View.GONE);
				}else{
					layout_out_module_message.setVisibility(View.VISIBLE);
					checkbox_isall_inner_module.setChecked(false);
					initSpinner_module_capacity(Long.valueOf(id).intValue());
					
					if(id==4){
						layout_inner_module_message.setVisibility(View.GONE);
						layout_inner_module_message_details.setVisibility(View.GONE);
						layout_ioConnectionLength_details.setVisibility(View.GONE);
					}else{
						layout_inner_module_message.setVisibility(View.VISIBLE);
						layout_inner_module_message_details.setVisibility(View.VISIBLE);
						layout_ioConnectionLength_details.setVisibility(View.VISIBLE);
					}
				}
				break;
			case R.id.spinner_module_capacity://室外机额定容量，判断GMV ES侧出风机，GMV Star家用多联室外机
				switch (Spinner_module_name.getSelectedItemPosition()) {
				case 1://GMV ES
					if(spinner_module_capacity.getSelectedItemPosition()> 8 ){//GMV ES侧出风机
						layout_inner_module_message.setVisibility(View.GONE);
						layout_inner_module_message_details.setVisibility(View.GONE);
						layout_ioConnectionLength_details.setVisibility(View.GONE);
						layout_row_text6.setVisibility(View.VISIBLE);
					}else{//GMV ES普通机型
						layout_inner_module_message.setVisibility(View.VISIBLE);
						layout_inner_module_message_details.setVisibility(View.VISIBLE);
						layout_ioConnectionLength_details.setVisibility(View.VISIBLE);
					}
					break;
				case 4://GMV Star
					Double capacity = Double.valueOf(spinner_module_capacity.getSelectedItemPosition()>0?spinner_module_capacity.getSelectedItem().toString():"0");
					if(capacity >= 20 ){
						layout_row_text6.setVisibility(View.VISIBLE);
					}else{
						layout_row_text6.setVisibility(View.GONE);
					}
					break;	
				default:
					break;
				}
				
				break;
			case R.id.spinner_module_capacity_1:
				if(id==0){
					spinner_module_capacity_2.setSelection(0);
					spinner_module_capacity_2.setEnabled(false);
					spinner_module_capacity_3.setSelection(0);
					spinner_module_capacity_3.setEnabled(false);
					spinner_module_capacity_4.setSelection(0);
					spinner_module_capacity_4.setEnabled(false);
				}else{
					spinner_module_capacity_2.setEnabled(true);
					spinner_module_capacity_2.setClickable(true);
				}
				break;
			case R.id.spinner_module_capacity_2:
				if(id==0){
					spinner_module_capacity_3.setSelection(0);
					spinner_module_capacity_3.setEnabled(false);
					spinner_module_capacity_4.setSelection(0);
					spinner_module_capacity_4.setEnabled(false);
				}else{
					spinner_module_capacity_3.setEnabled(true);
					spinner_module_capacity_3.setClickable(true);
				}
				break;
			case R.id.spinner_module_capacity_3:
				if(id==0){
					spinner_module_capacity_4.setSelection(0);
					spinner_module_capacity_4.setEnabled(false);
				}else{
					spinner_module_capacity_4.setEnabled(true);
					spinner_module_capacity_4.setClickable(true);
				}
				break;
			default:
				
				break;
			}
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub
			
		}

		
	};
	
	
	private OnClickListener mOnClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_back:
				finish();
				break;
			case R.id.btn_right_first:// 帮助
				Intent intent = new Intent(context, SupportMidActivity.class);
				intent.putExtra(HctConstants.ON_ACTIVITY_KEY_INFO_TYPE,
						SupportMidActivity.COLD_CALCULATOR);
				startActivity(intent);
				break;
			case R.id.scan_btn:// 二维码扫描
				intent = new Intent(context, CaptureActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivityForResult(intent, ZXING_DATA);
				break;
			case R.id.pic_btn:// 读取图库二维码
				ImageCropUtil util = new ImageCropUtil();
				util.doCropImage(context, FLAG_MODIFY_FINISH);
				break;	
			case R.id.sub_btn:// 提交计算
				DecimalFormat df = new DecimalFormat("0.00");//保留小数点2位
				int moduleId = Spinner_module_name.getSelectedItemPosition();
				if(moduleId==0){
					Toast.makeText(context,
							getString(R.string.toast_NotSelectModule),
							Toast.LENGTH_SHORT).show();
					return;
				}else if(moduleId==4){
					if(spinner_module_capacity.getSelectedItemPosition()==0){
						Toast.makeText(context,
								getString(R.string.barcode_info_is_incomplete),
								Toast.LENGTH_SHORT).show();
						return; 
					}
					double C = 0;
					Double capacity = Double.valueOf(spinner_module_capacity.getSelectedItem().toString());
					double F = (row_text7.getText()!= null && !row_text7.getText().toString().isEmpty())? Double.valueOf(row_text7.getText().toString()):0;
					double E = (row_text8.getText()!= null && !row_text8.getText().toString().isEmpty())? Double.valueOf(row_text8.getText().toString()):0;
					if(capacity >= 20 ){
						double G = (row_text6.getText()!= null && !row_text6.getText().toString().isEmpty())? Double.valueOf(row_text6.getText().toString()):0;
						C = RefrigerantCalculatorUtil.getRefrigerantCount_module_else(E,F,G);
					}else{
						C = RefrigerantCalculatorUtil.getRefrigerantCount_module4(F, E);
					}
					initAlertDialog(df.format(C).toString());
				}else if(moduleId==1 && spinner_module_capacity.getSelectedItemPosition()> 8){//侧风机
					double C = 0;
					double F = (row_text7.getText()!= null && !row_text7.getText().toString().isEmpty())? Double.valueOf(row_text7.getText().toString()):0;
					double E = (row_text8.getText()!= null && !row_text8.getText().toString().isEmpty())? Double.valueOf(row_text8.getText().toString()):0;
					double G = (row_text6.getText()!= null && !row_text6.getText().toString().isEmpty())? Double.valueOf(row_text6.getText().toString()):0;
					C = RefrigerantCalculatorUtil.getRefrigerantCount_module_else(E,F,G);
					initAlertDialog(df.format(C).toString());
				}else{
					/*
					 * A:模块制冷剂追加量
					 * B:配管制冷剂追加量
					 */
					double A = 0,B = 0;
					if(!checkbox_isall_inner_module.isChecked()){//如果勾选系统全部为新风室内机，则A值为0,没有勾选，则计算A值
						int inner_count =0;
						double inner_capacityCount=0;
						try {
							//获取内机总数
							inner_count = Integer.valueOf(editText_inner_count.getText().toString());
							//获取内机额定容量
							inner_capacityCount = Double.valueOf(editText_inner_capacityCount.getText().toString());
						} catch (Exception e) {
							Toast.makeText(context,
									getString(R.string.barcode_info_is_incomplete),
									Toast.LENGTH_SHORT).show();
							return; 
						}
						ArrayList<Double> module_capacity = new ArrayList<Double>();
						ArrayList<Integer> module_capacity_position = new ArrayList<Integer>();
						if(moduleId==2){//多外机模块
							if(spinner_module_capacity_1.getSelectedItemId()!=0){
								Double capacity = Double.valueOf(spinner_module_capacity_1.getSelectedItem().toString());
								module_capacity.add(capacity);
								module_capacity_position.add(spinner_module_capacity_1.getSelectedItemPosition()-1);//需要-1，排除“无”的情况
							}
							if(spinner_module_capacity_2.getSelectedItemId()!=0){
								Double capacity = Double.valueOf(spinner_module_capacity_2.getSelectedItem().toString());
								module_capacity.add(capacity);
								module_capacity_position.add(spinner_module_capacity_2.getSelectedItemPosition()-1);
							}
							if(spinner_module_capacity_3.getSelectedItemId()!=0){
								Double capacity = Double.valueOf(spinner_module_capacity_3.getSelectedItem().toString());
								module_capacity.add (capacity);
								module_capacity_position.add(spinner_module_capacity_3.getSelectedItemPosition()-1);
							}
							if(spinner_module_capacity_4.getSelectedItemId()!=0){
								Double capacity = Double.valueOf(spinner_module_capacity_4.getSelectedItem().toString());
								module_capacity.add (capacity);
								module_capacity_position.add(spinner_module_capacity_4.getSelectedItemPosition()-1);
							}
							
						}else{//单外机模块
							if(spinner_module_capacity.getSelectedItemId()!=0){
								Double capacity = Double.valueOf(spinner_module_capacity.getSelectedItem().toString());
								module_capacity.add(capacity);
								module_capacity_position.add(spinner_module_capacity.getSelectedItemPosition()-1);
							}
						}
						
						int rate_position = RefrigerantCalculatorUtil.getRate(inner_capacityCount, module_capacity); 
						if(rate_position == -1){
							Toast.makeText(context,
									getString(R.string.erro_RefrigerantCalculator),
									Toast.LENGTH_SHORT).show();
							return;
						}
						A = RefrigerantCalculatorUtil.getRefrigerantCount_A(moduleId,module_capacity_position, inner_count, rate_position);
						
						Log.d("rate", String.valueOf(rate_position));
						Log.d("refrigerant", String.valueOf(A));
					}
					//计算B值
					double[] ioConnectionLengthList = {
							(row_text1.getText()!= null && !row_text1.getText().toString().isEmpty())? Double.valueOf(row_text1.getText().toString()):0,
							(row_text2.getText()!= null && !row_text2.getText().toString().isEmpty())? Double.valueOf(row_text2.getText().toString()):0,
							(row_text3.getText()!= null && !row_text3.getText().toString().isEmpty())? Double.valueOf(row_text3.getText().toString()):0,
							(row_text4.getText()!= null && !row_text4.getText().toString().isEmpty())? Double.valueOf(row_text4.getText().toString()):0,
							(row_text5.getText()!= null && !row_text5.getText().toString().isEmpty())? Double.valueOf(row_text5.getText().toString()):0,
							(row_text6.getText()!= null && !row_text6.getText().toString().isEmpty())? Double.valueOf(row_text6.getText().toString()):0,
							(row_text7.getText()!= null && !row_text7.getText().toString().isEmpty())? Double.valueOf(row_text7.getText().toString()):0,
							(row_text8.getText()!= null && !row_text8.getText().toString().isEmpty())? Double.valueOf(row_text8.getText().toString()):0
					};
					
					B = RefrigerantCalculatorUtil.getRefrigerantCount_B(ioConnectionLengthList);
					Double C = A + B;
					
					initAlertDialog(df.format(C).toString());
					
				}
				
				break;	
				
				
			default:
				break;
			}
		}

	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != context.RESULT_OK)
			return;
		if (requestCode == FLAG_MODIFY_FINISH && resultCode == RESULT_OK) {
			if (data != null) {
				ImageCropUtil cropUtil = new ImageCropUtil();
				readerFromSDCardPath(cropUtil.getTmpFilePath());
			}

		} else {//扫描条码返回
			String result = data.getStringExtra("result");
			if (result == null) {
				return;
			}
			code_edit.setText(result);
			Utils.setLastSelection(code_edit);
		}
		changeModuleFromBarcode();
	}

	private void readerFromSDCardPath(String picturePath) {
		try {
			DecodeBitmapUtil decodeBitmapUtil = new DecodeBitmapUtil();
			Bitmap bitmap = decodeBitmapUtil.getHorizontalBitmap(picturePath);

			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
			RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
			BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
			MultiFormatReader reader2 = new MultiFormatReader();
			Result result;
			try {
				result = reader2.decode(bitmap1, hints);
				code_edit.setText(result.getText());
				Utils.setLastSelection(code_edit);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Utils.showToastCenter(context,
						getString(R.string.not_found_the_barcode_hint));
			}
		} catch (Exception e) {
		}
	}
	
	
	/**
	 * 自定义弹出框
	 * @param activity
	 */
	protected  void initAlertDialog( final String refrigerant) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
	    
	    if(getIntent().getStringExtra("barcode")!=null){
	    	builder.setPositiveButton(getString(R.string.ok_RefrigerantCalculator), new DialogInterface.OnClickListener() { 
	    		public void onClick(DialogInterface dialog, int id) { 
	    			Intent data = new Intent().putExtra("RefrigerantCalculator", refrigerant);
	    			setResult(RESULT_OK, data);
	    			dialog.cancel(); 
	    			finish();
	    		} 
	    	}); 
	    }
	    
	    builder.setTitle(getString(R.string.title_Dialog_RefrigerantCalculator))
	    	   .setMessage(refrigerant + " kg") 
	           .setCancelable(false) 
	           .setNegativeButton(getString(R.string.cancel_hint), new DialogInterface.OnClickListener() { 
	               public void onClick(DialogInterface dialog, int id) { 
	                    dialog.cancel(); 
	               } 
	           }); 
	    
	    
	    AlertDialog alert = builder.create();
	    alert.show();
	}
	
}
